import ArrayUtil from '@/ArrayUtil';
import BaseUtil from '@/BaseUtil';
import Buff from '@/Buff';
import BuffUtil from '@/BuffUtil';
import SelectUtil from '@/SelectUtil';
import { OrderId } from '@/globals/order';
import { Trigger } from '@/trigger';
import BuffCommon from 'code/Core/Common/BuffCommon';
import SelectCommon from 'code/Core/Common/SelectCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 圣言术 - 耀
 **/
export default class MS_HolyShine extends AbilityBase {
    static instance: MS_HolyShine;
    buffType: AppBuffType = {
        id: 'buff_MS_HolyShine',
        art: this.art,
        name: this.abilityName,
        ubertip: '持续地治疗或者伤害',
        dur: 2,
        class: '光环',
        targetAttach: 'foot',
        targetArt: 'Abilities\\Spells\\Other\\ANrm\\ANrmTarget.mdl',
        interval: 1,
        onInterval: this.onBuffInterval.bind(this),
    };
    onBuffInterval(buff: Buff) {
        let lv = this.getAbilityLevel(buff.getCaster());
        let dmg = 17 + UnitCommon.取法强(buff.getCaster()) * 0.3 * lv;
        let ally = IsUnitAlly(buff.getParent(), GetOwningPlayer(buff.getCaster()));
        if (ally) {
            DamageSystemInstance.applyDotHeal(buff.getParent(), buff.getCaster(), dmg, this.id);
        } else {
            DamageSystemInstance.applyLightDamage(buff.getParent(), buff.getCaster(), dmg, this.id, true);
        }
    }
    constructor(id: string) {
        super(id);
        MS_HolyShine.instance = this;
        this.init();
        let t = new Trigger();
        t.registerAnyUnitEvent(EVENT_PLAYER_UNIT_ISSUED_ORDER);
        t.addAction(this.CommandValidate.bind(this));
        BaseUtil.onTimer(1, this.timerEffect.bind(this));
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
    }
    providers: unit[] = [];
    CommandValidate() {
        let tru = GetTriggerUnit();
        if (this.getAbilityLevel(tru) == 0) return;
        if (GetUnitAbilityLevel(tru, this.id) <= 0) return;
        let order = GetIssuedOrderId();
        if (order == OrderId.Immolation) {
            this.providers.push(tru);
        } else if (order == OrderId.Unimmolation) {
            ArrayUtil.removeElement(this.providers, tru);
        }
    }
    timerEffect() {
        for (let p of this.providers) {
            let lv = GetUnitAbilityLevel(p, this.id);
            let mana = GetUnitState(p, UNIT_STATE_MANA);
            if (mana < lv * 10) {
                ArrayUtil.removeElement(this.providers, p);
                continue;
            }
            let us = SelectCommon.getAllyUnitInRange(p, 450);
            for (let u of us) {
                BuffCommon.addBuff({ target: u, caster: p, ablityId: this.id, buffTypeId: this.buffType.id });
            }
            let es = SelectUtil.getEnemyUnitsInRange(p, 450);
            for (let u of es) {
                BuffCommon.addBuff({ target: u, caster: p, ablityId: this.id, buffTypeId: this.buffType.id });
            }
        }
        return true;
    }
}
